﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ArchivingDataModel;
using ArchivingDataWebForms.Helpers;
using Telerik.Web.UI;

namespace ArchivingDataWebForms.Pages.Folders
{
    public partial class ViewFoldersGrid : WebFormBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void AdvancedRadGridResults_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            using (ArchivingDataEntities ent = new ArchivingDataEntities())
            {
                var items = ent.Folders;
                this.AdvancedRadGridResults.DataSource = items;
            }
        }
        protected void AdvancedRadGridResults_ItemCommand(object sender, GridCommandEventArgs e)
        {
        }
        protected void AdvancedRadGridResults_InsertCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                using (ArchivingDataEntities ent = new ArchivingDataEntities())
                {
                    GridEditableItem editedItem = e.Item as GridEditableItem;
                    Hashtable newValues = new Hashtable();
                    e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
                    if (!string.IsNullOrEmpty(this.Request.QueryString[QueryStringParams.Folder_ID]))
                    {
                        long id = Convert.ToInt64(this.Request.QueryString[QueryStringParams.Folder_ID]);
                        string code = newValues["Folder_code"].ToString();
                        string nameAr = newValues["FolderName_Ar"].ToString();
                        string noteAr = newValues["FolderNote_Ar"].ToString();
                        string uniquetext = WebHelper.Helpers.StringHelper.ToUniqueStatement(code + nameAr  + noteAr);
                        ent.AddToFolders(new Folder
                        {
                            Folder_Code = code,
                            FolderName_Ar = nameAr,
                            FolderNote_Ar = noteAr,
                            Folder_UniqueText = uniquetext
                        });
                    }
                    ent.SaveChanges();
                    this.ShowNotification("تم الحفظ بنجاح", NotificationIconType.Ok);
                }
            }
            catch (Exception er)
            {
                this.ShowNotification("حدث الخطا التالي اثناء الحفظ " + er.Message, NotificationIconType.Error);
                e.Canceled = true;
                return;
            }
        }
        protected void AdvancedRadGridResults_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                using (ArchivingDataEntities ent = new ArchivingDataEntities())
                {
                    GridEditableItem editedItem = e.Item as GridEditableItem;
                    Hashtable newValues = new Hashtable();
                    e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
                    if (!string.IsNullOrEmpty(this.Request.QueryString[QueryStringParams.Folder_ID]))
                    {
                        long id = Convert.ToInt64(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Folder_ID"]);
                        string code = newValues["Folder_code"].ToString();
                        string nameAr = newValues["FolderName_Ar"].ToString();
                        string noteAr = newValues["FolderNote_Ar"].ToString();
                        string uniquetext = WebHelper.Helpers.StringHelper.ToUniqueStatement(code + nameAr + noteAr);
                        var item = ent.Folders.FirstOrDefault(i => i.Folder_ID == id);
                        item.Folder_Code = code;
                        item.FolderName_Ar = nameAr;
                        item.FolderNote_Ar = noteAr;
                        item.Folder_UniqueText = uniquetext;
                    }
                    ent.SaveChanges();
                    this.ShowNotification("تم الحفظ بنجاح", NotificationIconType.Ok);
                }
            }
            catch (Exception er)
            {
                this.ShowNotification("حدث الخطا التالي اثناء الحفظ " + er.Message, NotificationIconType.Error);
                e.Canceled = true;
                return;
            }
        }
        protected void AdvancedRadGridResults_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                using (ArchivingDataEntities ent = new ArchivingDataEntities())
                {
                    if (!string.IsNullOrEmpty(this.Request.QueryString[QueryStringParams.Folder_ID]))
                    {
                        long id = Convert.ToInt64(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Folder_ID"]);
                        long folderID = Convert.ToInt64(this.Request.QueryString[QueryStringParams.Folder_ID]);
                        ent.DeleteObject(ent.Folders.FirstOrDefault(i => i.Folder_ID == id));
                    }
                    ent.SaveChanges();
                    this.ShowNotification("تم الحذف بنجاح", NotificationIconType.Ok);
                }
            }
            catch (Exception er)
            {
                this.ShowNotification("حدث الخطا التالي اثناء الحذف " + er.Message, NotificationIconType.Error);
                e.Canceled = true;
                return;
            }
        }
        protected void AdvancedRadGridResults_ItemDataBound(object sender, GridItemEventArgs e)
        {
            //if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            //{
            //    //width
            //    GridEditableItem editedItem = e.Item as GridEditableItem;
            //    GridEditManager editMan = editedItem.EditManager;
            //    GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor)(editMan.GetColumnEditor("FolderNote_Ar"));
            //    editor.TextBoxMode = TextBoxMode.MultiLine;
            //    editor.TextBoxControl.Width = Unit.Pixel(300);
            //    editor.TextBoxControl.Height = Unit.Pixel(40);
            //    GridTextBoxColumnEditor editor2 = (GridTextBoxColumnEditor)(editMan.GetColumnEditor("FolderNote_En"));
            //    editor2.TextBoxMode = TextBoxMode.MultiLine;
            //    editor2.TextBoxControl.Width = Unit.Pixel(300);
            //    editor2.TextBoxControl.Height = Unit.Pixel(40);
            //}
        }

    }
}